package com.qmx.launcher.activity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.RectF;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Toast;

import com.open.androidtvwidget.bridge.RecyclerViewBridge;
import com.open.androidtvwidget.leanback.adapter.GeneralAdapter;
import com.open.androidtvwidget.leanback.recycle.LinearLayoutManagerTV;
import com.open.androidtvwidget.leanback.recycle.RecyclerViewTV;
import com.open.androidtvwidget.view.MainUpView;
import com.qmx.launcher.R;
import com.qmx.launcher.activity.base.BaseActivity;
import com.qmx.launcher.activity.general.TerminalActivity;
import com.qmx.launcher.adapter.SettingRecyclerViewPresenter;
import com.qmx.launcher.view.CustomerOnOff;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * @author ChengXiangtong
 * @create 2018/10/31
 * @Describe 常规
 */
public class GeneralActivity extends BaseActivity implements RecyclerViewTV.OnItemListener, RecyclerViewTV.OnChildViewHolderSelectedListener {

    private Context mContext;
    private RecyclerViewTV mRecyclerView;
    private MainUpView mainUpView1;
    private RecyclerViewBridge mRecyclerViewBridge;
    private View oldView;
    private SettingRecyclerViewPresenter mRecyclerViewPresenter;
    private GeneralAdapter mGeneralAdapter;
    public int requestCode = 0;
    /**
     * 默认选中的位置
     */
    private int mSavePos = 0;
    private String str[] = new String[]{"终端名称", "终端号", "服务器地址", "语音", "支持触屏操作", "开机快速自检", "进入屏保时间", "进入休眠时间", "关于"};
    private HashMap<Integer, String> itemContent = new HashMap<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_general);
        //        customerOnOff = (CustomerOnOff)findViewById(R.id.custom_onoff);
        ////
        //        customerOnOff.setOnClickListener(new CustomerOnOff.onClickListener() {
        //            @Override
        //            public void onLeftRowClick() {
        //                Toast.makeText(SettingActivity.this, "点击关箭头", Toast.LENGTH_SHORT).show();
        //                customerOnOff.stateChange("关");
        //            }
        //
        //            @Override
        //            public void onRightRowClick() {
        //                Toast.makeText(SettingActivity.this, "onRightRowClick点击开箭头", Toast.LENGTH_SHORT).show();
        //                customerOnOff.stateChange("开");
        //            }
        //        });
        mContext = GeneralActivity.this;
        mRecyclerView = (RecyclerViewTV) findViewById(R.id.recyclerView);
        mainUpView1 = (MainUpView) findViewById(R.id.mainUpView1);
        mainUpView1.setEffectBridge(new RecyclerViewBridge());
        // 注意这里，需要使用 RecyclerViewBridge 的移动边框 Bridge.
        mRecyclerViewBridge = (RecyclerViewBridge) mainUpView1.getEffectBridge();
        mRecyclerViewBridge.setUpRectResource(R.drawable.white_light_10);
        float density = getResources().getDisplayMetrics().density;
        RectF receF = new RectF(getDimension(R.dimen.w_30) * density, getDimension(R.dimen.h_20) * density,
                getDimension(R.dimen.w_30) * density, getDimension(R.dimen.h_20) * density);
        mRecyclerViewBridge.setDrawUpRectPadding(receF);
        recyclerViewLinerLayout(RecyclerView.VERTICAL);
        //
        mRecyclerView.setOnItemListener(this);
        mRecyclerView.setOnChildViewHolderSelectedListener(this);
        // item 单击事件处理.
        mRecyclerView.setOnItemClickListener(new RecyclerViewTV.OnItemClickListener() {
            @Override
            public void onItemClick(RecyclerViewTV parent, View itemView, int position) {
                Toast.makeText(mContext, "点击位置" + position, Toast.LENGTH_SHORT).show();
                requestCode = position;
                enterSelectedActivty(position);
            }
        });

    }


    public void enterSelectedActivty(int position) {
        Intent intent = new Intent();
        switch (position) {
            case 0:
                intent.setClass(mContext, TerminalActivity.class);
                break;
            case 1:
                intent.setClass(mContext, GeneralActivity.class);
                break;
            case 2:
                intent.setClass(mContext, GeneralActivity.class);
                break;
            case 3:
                intent.setClass(mContext, GeneralActivity.class);
                break;
            case 4:
                intent.setClass(mContext, GeneralActivity.class);
                break;
            case 5:
                intent.setClass(mContext, GeneralActivity.class);
                break;
            case 6:
                intent.setClass(mContext, GeneralActivity.class);
                break;
            case 7:
                intent.setClass(mContext, GeneralActivity.class);
                break;
            default:
                return;
        }
        startActivityForResult(intent, requestCode);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch (requestCode) {
            case 0:
                itemContent.put(requestCode, data.getDataString());
                break;
            case 1:

                break;
            case 2:

                break;
            case 3:

                break;
            case 4:

                break;
            case 5:

                break;
            case 6:

                break;
            case 7:

                break;
            default:
                return;
        }
        //        mGeneralAdapter.notifyItemChanged(requestCode);
        //        super.onActivityResult(requestCode, resultCode, data);
    }

    private void recyclerViewLinerLayout(int orientation) {
        LinearLayoutManagerTV layoutManager = new LinearLayoutManagerTV(this);
        layoutManager.setOrientation(orientation);
        mRecyclerView.setLayoutManager(layoutManager);
        mRecyclerView.setFocusable(false);
        mRecyclerViewPresenter = new SettingRecyclerViewPresenter(str);
        mGeneralAdapter = new GeneralAdapter(mRecyclerViewPresenter);
        mRecyclerView.setAdapter(mGeneralAdapter);
        mRecyclerView.setDefaultSelect(mSavePos);
    }

    @Override
    public void onItemPreSelected(RecyclerViewTV parent, View itemView, int position) {
        mRecyclerViewBridge.setUnFocusView(oldView);
    }

    @Override
    public void onItemSelected(RecyclerViewTV parent, View itemView, int position) {
        //        mRecyclerViewBridge.setFocusView(itemView, 1.05f);
        oldView = itemView;
    }

    @Override
    public void onReviseFocusFollow(RecyclerViewTV parent, View itemView, int position) {
        //        mRecyclerViewBridge.setFocusView(itemView, 1.05f);
        oldView = itemView;
    }

    @Override
    public void onChildViewHolderSelected(RecyclerView parent, RecyclerView.ViewHolder vh, int position) {

    }
}
